<?php
namespace Core;

class Server  extends Base {

    /**
     * @var \swoole_server
     */
    public $server;

    public $serverConfig;

    public $appConfig;

    public $swooleConfig;

    function start() {
        $pidfile = SWOOLE_PATH.'/server.pid';
        foreach ($this->serverConfig as $config) {
            if(!$this->server) {
                $this->server = new $config['class']($config['ip'],$config['port'],$config['mode'],$config['sock_type']);
                $this->server->set($config['swoole']);
                $this->server->set(['pid_file'=>$pidfile]);
                $this->server->set($this->swooleConfig);
                foreach ($config['on'] as $k=>$v) {
                    $this->server->on($k,$v);
                }
            }else {
                echo '多server目前版本暂时不支持';
                return;
            }
        }
        $this->server->start();
    }

    function stop() {
        $pid = file_get_contents(SWOOLE_PATH.'/server.pid');
        system('kill -15 '.$pid);
    }

    function reloadWorker() {
        $pid = file_get_contents(SWOOLE_PATH.'/server.pid');
        system('kill -USR1 '.$pid);
    }

}